package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletLanguageSpecificListInfor
import kotlinx.android.synthetic.main.aibk_adapter_item_specific_grammar.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_specific_language.view.*
import kotlinx.android.synthetic.main.aibk_dialog_specific_grammar_analysis.*

/*
* 语言能力
* */
class AibkGenerateSpecificLanguageAdapter(
        val activity: AppCompatActivity,
        val templetLanguageSpecificListInfor: TempletLanguageSpecificListInfor
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_specific_language, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
            val langName = templetLanguageSpecificListInfor[position].LangName
            val langScoreRate = templetLanguageSpecificListInfor[position].LangScoreRate
            val cognitionAbility = templetLanguageSpecificListInfor[position].CognitionAbility
            val cognitionEfficiency = templetLanguageSpecificListInfor[position].CognitionEfficiency
            val mastery = templetLanguageSpecificListInfor[position].Mastery

            //显示能力名称
            holder.convertView.TvLanguageAbility.text = langName + "能力"
            //显示分值占比
            holder.convertView.TvLangScoreRate.text = "$langScoreRate%"
            //显示认知能力
            holder.convertView.RecommendStarBar1.isClickable = false
            if (cognitionAbility > cognitionAbility) {
                holder.convertView.RecommendStarBar1.numStars = cognitionAbility.toInt() + 1
            } else {
                holder.convertView.RecommendStarBar1.numStars = cognitionAbility.toInt()
            }
            holder.convertView.RecommendStarBar1.rating = cognitionAbility.toFloat()
            //显示认知效率
            if(cognitionEfficiency == 0){
                holder.convertView.TvEmptyRzxl.visibility = View.VISIBLE
                holder.convertView.RecommendStarBar2.visibility = View.GONE
            }else {
                holder.convertView.TvEmptyRzxl.visibility = View.GONE
                holder.convertView.RecommendStarBar2.visibility = View.VISIBLE
                holder.convertView.RecommendStarBar2.isClickable = false
                if (cognitionEfficiency > cognitionEfficiency) {
                    holder.convertView.RecommendStarBar2.numStars = cognitionEfficiency + 1
                } else {
                    holder.convertView.RecommendStarBar2.numStars = cognitionEfficiency
                }
                holder.convertView.RecommendStarBar2.rating = cognitionEfficiency.toFloat()
            }
            //显示掌握率
            val masteryX = LancooMethod.getScoreRate(mastery, 2)
            holder.convertView.TvMastery.text = "$masteryX"
            //显示专项选中状态
            val isChecked = templetLanguageSpecificListInfor[position].IsChecked
            holder.convertView.LlLanguageSpecifiView.isSelected = isChecked
            holder.convertView.BtnCheckDetails.isSelected = isChecked
            holder.convertView.BtnCheckDetails.setOnClickListener {
                clickAbilityListener!!.onClickAnalysis(position)
            }
            holder.convertView.LlLanguageSpecifiView.setOnClickListener {
                holder.convertView.LlLanguageSpecifiView.isSelected = true
                holder.convertView.BtnCheckDetails.isSelected = true

                val languageSpecificListSize = templetLanguageSpecificListInfor.size
                for (i in 0 until languageSpecificListSize) {
                    templetLanguageSpecificListInfor[i].IsChecked = i == position
                }

                clickAbilityListener!!.onClickAbility(position)
                notifyDataSetChanged()
            }
    }

    override fun getItemCount(): Int {
        return templetLanguageSpecificListInfor.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private var clickAbilityListener: OnClickAbilityListener? = null

    /*
   * 设置点击事件
   * */
    interface OnClickAbilityListener {
        fun onClickAbility(pos:Int)
        fun onClickAnalysis(pos:Int)
    }

    fun setClickDialogListener(clickAbilityListener: OnClickAbilityListener?) {
        this.clickAbilityListener = clickAbilityListener
    }
}